const app = getApp()

Page({
  data: {
    //用于分页的属性
    totalPage : 1,
    page : 1,
    videoList: [],

    screenWidth: 350,
    serverUrl : "",

    searchContent: ""
  },

  onLoad: function (params) {
    var me = this;
    var screenWidth = wx.getSystemInfoSync().screenWidth;
    me.setData({
      screenWidth: screenWidth,
    });

    //获取当前分页
    var searchContent = params.search;
    var isSaveRecord = params.isSaveRecord;
    if(isSaveRecord == null || isSaveRecord == '' || isSaveRecord == undefined){
      isSaveRecord = 0;
    }
    me.setData({
      searchContent:searchContent
    });
    //获取当前分页数
    var page = me.data.page;
    me.getAllVideoList(page,isSaveRecord);    
  },

  //获取视频首页的视频
  getAllVideoList:function(page,isSaveRecord){
    var me = this;
    var serverUrl = app.serverUrl
    wx.showLoading({
      title: '请等待，加载中',
    })

    var searchContent = me.data.searchContent;

    wx.request({
      url: serverUrl + "/video/showAll.do?page=" + page + "&isSaveRecord=" + isSaveRecord ,
      method: "POST",
      data:{
        videoDesc: searchContent
      },
      success: function(res){
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        
        console.log(res.data);
        //判断是否为第一页，若是则令videoList[]为空
        if(page === 1 ){
          me.setData({
            videoList: []
          });
        }
        //原有的视频数据
        var videoList = res.data.data.rows;
        //新刷到的视频数据
        var newVideoList = me.data.videoList;

        //将原有的视频数据与新视频数据组合，展示
        me.setData({
          videoList: newVideoList.concat(videoList),
          page: page,
          totalPage: res.data.data.total,
          serverUrl: serverUrl
        });

      }
    })
  },

  onPullDownRefresh:function(){
    wx.showNavigationBarLoading();
    this.getAllVideoList(1);
  },

  onReachBottom:function(){
    var me = this;
    var page = me.data.page;
    var totalPage = me.data.totalPage;
    //判断当前页数与总页数是否相等。 若相等则无需查询 
    if(page === totalPage){
      wx.showToast({
        title: '已经没有视频啦！',
        icon: 'none',
      })
      return
    }
    //查询下一页
    page += 1
    
    me.getAllVideoList(page);
  },

  showVideoInfo:function(e){
    var me = this;
    var videoList = me.data.videoList;
    var arrindex = e.target.dataset.arrindex;
    var videoInfo = JSON.stringify(videoList[arrindex]);

    wx.redirectTo({
      url: '../videoInfo/videoInfo?videoInfo=' + videoInfo
    })
  }


})
